package com.daffodil.workable.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.daffodil.core.dao.JpaDao;
import com.daffodil.core.entity.Query;
import com.daffodil.workable.entity.WorkableForm;
import com.daffodil.workable.service.IWorkableFormService;
import com.daffodil.util.HqlUtils;
import com.daffodil.util.StringUtils;

/**
 * -节点表单Service接口业务实现层
 * @author yweijian
 * @date 2022-04-07
 * @version 1.0
 * @description
 */
@Service
public class WorkableFormServiceImpl implements IWorkableFormService {

    @Autowired
    private JpaDao<String> jpaDao;

    @Override
    public List<WorkableForm> selectWorkableFormList(Query<WorkableForm> query) {
        StringBuffer hql = new StringBuffer("from WorkableForm where 1=1 ");
        List<Object> paras = new ArrayList<Object>();
        HqlUtils.createHql(hql, paras, query);
        return jpaDao.search(hql.toString(), paras, WorkableForm.class, query.getPage());
    }

    @Override
    public WorkableForm selectWorkableFormById(String id) {
        return jpaDao.find(WorkableForm.class, id);
    }

    @Override
    @Transactional
    public void insertWorkableForm(WorkableForm workableForm) {
        jpaDao.save(workableForm);
    }

    @Override
    @Transactional
    public void updateWorkableForm(WorkableForm workableForm) {
        jpaDao.update(workableForm);
    }

    @Override
    @Transactional
    public void deleteWorkableFormByIds(String[] ids) {
        jpaDao.delete(WorkableForm.class, ids);
    }

    @Override
    public WorkableForm selectWorkableFormByTaskIdAndNodeId(String taskId, String nodeId) {
        if(StringUtils.isEmpty(taskId) || StringUtils.isEmpty(nodeId)) {
            return null;
        }
        List<Object> paras = new ArrayList<Object>();
        paras.add(taskId);
        paras.add(nodeId);
        return jpaDao.find("from WorkableForm where taskId = ? and nodeId = ?", paras, WorkableForm.class);
    }

}
